package com.xuetang9.fujian.view;


import com.xuetang9.fujian.model.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;

/**
 * @author: fujian
 * @description: 登录主面板
 * @date: 2021/6/7 9:38
 * @version: V1.0
 * @package: com.xuetang9.fujian.view
 **/
@SuppressWarnings("all")
public class LoginFrame extends JFrame {

    private JPanel contentPanel = null;

    private JPanel loginPanel = new JPanel(null);
    private JLabel lblHead = new JLabel();
    private JComboBox<String> cmbAccount = new JComboBox<>();
    private JPasswordField passwordField = new JPasswordField();
    private JCheckBox autoLogin = new JCheckBox("自动登录");
    private JCheckBox remeberPwd = new JCheckBox("记住密码");
    private JButton btnFindPwd = new JButton("<html><u>找回密码<u/><html/>");
    private JButton btnRegister = new JButton("<html><u>注册<u/><html/>");
    private JButton btnCode = new JButton();
    private JButton btnLogin = new JButton("<html><b>登  录<b/><html/>");
    private Color textColor = new Color(166,166,166);
    private Font textFont = new Font("微软雅黑",Font.PLAIN,12);
    private Font loginButtonTextFont = new Font("微软雅黑",Font.PLAIN,16);
    private Color loginButtonTextColor = new Color(Color.BITMASK);
    public LoginFrame(){
        init();
        setVisible(true);
    }

    private void init() {
        setSize(436,336);
        setTitle("登录");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        contentPanel = (JPanel) getContentPane();
        // 配置所需组件
        configLoginPane();
        configAccount();
        configPassword();
        configAutoLogin();
        configRemember();
        configFindPwd();
        configLoginOpt();
        configRegister();
        configLoginCode();
        configHeadLabel();
        // 组装所所有的组件
        installComponents();
        // 组装所需组件的事件监听
        installListeners();
    }

    private void configLoginPane() {
        loginPanel.setBackground(Color.CYAN);
        loginPanel.setPreferredSize(new Dimension(0,200));
    }

    private void configAccount() {
        cmbAccount.setBounds(90,30,238,35);
        cmbAccount.setBackground(Color.WHITE);
        cmbAccount.setEditable(true);
        cmbAccount.setMaximumRowCount(5);
    }

    private void configPassword() {
        passwordField.setBounds(90,75,238,35);
        passwordField.setBackground(Color.WHITE);
    }

    private void configAutoLogin() {
        autoLogin.setBounds(90,107,80,25);
        autoLogin.setOpaque(false);
        autoLogin.setForeground(textColor);
        autoLogin.setFont(textFont);
    }

    private void configRemember() {
        remeberPwd.setBounds(180,107,80,25);
        remeberPwd.setOpaque(false);
        remeberPwd.setForeground(textColor);
        remeberPwd.setFont(textFont);
    }

    private void configFindPwd() {
        btnFindPwd.setBounds(263,107,80,25);
        btnFindPwd.setOpaque(false);
        btnFindPwd.setContentAreaFilled(false);
        btnFindPwd.setFocusPainted(false);
        btnFindPwd.setBorder(null);
        btnFindPwd.setFont(textFont);
        btnFindPwd.setForeground(textColor);
    }

    private void configLoginOpt() {
        btnLogin.setBounds(90,140,238,40);
        btnLogin.setOpaque(false);
        btnLogin.setForeground(loginButtonTextColor);
        btnLogin.setFont(loginButtonTextFont);
        btnLogin.setContentAreaFilled(false);
    }

    private void configRegister() {
        btnRegister.setBounds(15,170,70,20);
        btnRegister.setOpaque(false);
        btnRegister.setContentAreaFilled(false);
        btnRegister.setFocusPainted(false);
        btnRegister.setBorder(null);
        btnRegister.setForeground(Color.BLACK);
        btnRegister.setFont(textFont);
    }

    private void configLoginCode() {
        btnCode.setBounds(383,170,22,22);
        btnCode.setContentAreaFilled(false);
    }

    public void configHeadLabel(){
        lblHead.setBounds(170,50,65,65);
        lblHead.setOpaque(true);
        lblHead.setBackground(Color.BLACK);

    }

    private void installComponents() {
        loginPanel.add(cmbAccount);
        loginPanel.add(passwordField);
        loginPanel.add(autoLogin);
        loginPanel.add(remeberPwd);
        loginPanel.add(btnFindPwd);
        loginPanel.add(btnLogin);
        loginPanel.add(btnRegister);
        loginPanel.add(btnCode);
        contentPanel.add(loginPanel,BorderLayout.SOUTH);
        getLayeredPane().add(lblHead,JLayeredPane.DEFAULT_LAYER);
    }

    private void installListeners() {
        btnFindPwd.addMouseListener(new FindPasswordButtonHandler());
        btnFindPwd.addActionListener(new FindPasswordButtonHandler());
        btnRegister.addActionListener(new RegisterButtonHandler());
        btnRegister.addMouseListener(new RegisterButtonHandler());
        btnLogin.addActionListener(new LoginButtonHandler());
        btnLogin.addMouseListener(new LoginButtonHandler());
        addWindowListener(new WindowCloseHandler());
    }

    public class FindPasswordButtonHandler extends MouseAdapter implements ActionListener{

        @Override
        public void mouseEntered(MouseEvent e) {
            btnFindPwd.setForeground(Color.red);
            btnFindPwd.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            btnFindPwd.setForeground(textColor);
            btnFindPwd.setFont(textFont);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

        }
    }

    private class RegisterButtonHandler extends MouseAdapter implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            LoginFrame.this.setVisible(false);
            WindowManager.addRegisterFrame(new RegisterFrame(LoginFrame.this));
        }
        @Override
        public void mouseEntered(MouseEvent e) {
            btnRegister.setForeground(Color.red);
            btnRegister.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            btnRegister.setForeground(textColor);
            btnRegister.setFont(textFont);
        }
    }

    private class LoginButtonHandler extends MouseAdapter implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            // send login message
            HashMap<String, Object> message = new HashMap<>();
            message.put("handlerClass","com.xuetang9.fujian.net.request.LoginRequestHandler");
            User user = new User();
            String account = cmbAccount.getSelectedItem().toString();
            String password = new String(passwordField.getPassword());
            user.setAccount(account);
            user.setPassword(password);
            // yinggaifasong account he password
            message.put("loginUser",user);
            MainFrame.client.sendMessage(message);
            WindowManager.addLoginFrame(LoginFrame.this);
            //load mainFrame
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            btnLogin.setForeground(Color.red);
            btnLogin.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            btnLogin.setForeground(loginButtonTextColor);
            btnLogin.setFont(loginButtonTextFont);
        }

    }

    private class WindowCloseHandler extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e) {
            // send close message
            HashMap<String, Object> message = new HashMap<>();
            message.put("closeFrame","LoginFrame");
            message.put("exit",true);
            message.put("handlerClass","com.xuetang9.fujian.net.request.ClientExitRequestHandler");
            MainFrame.client.sendMessage(message);
        }
    }

    public static void main(String[] args) {
        new LoginFrame();
    }
}
